﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.IO;
using System.Text.RegularExpressions;


namespace 易览漫画
{
    public partial class UpLoad : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {



        }

        protected void submit_Click(object sender, EventArgs e)
        {
            string fenlei = this.tushufenlei.SelectedValue;

            string zuozhe = this.tushuzuozhe.Text;
            if (zuozhe=="")
            {
                Response.Write("<script >function window.onload() {alert('请填写作者信息!');}</script>"); 
                return;
            }

            string mingcheng = this.tushumingcheng.Text;
            if (mingcheng == "")
            {
                Response.Write("<script >function window.onload() {alert('请填写图书名称!');}</script>");
                return;
            }
            
            string riqi = this.chubanriqi.Text;
            DateTime datett;
            bool b = DateTime.TryParse(riqi,out datett);
            if (riqi == ""||!b)
            {
                Response.Write("<script >function window.onload() {alert('出版日期错误!');}</script>");
                return;
            }
            string jiage = this.tushujiage.Text;
            decimal jia;
            bool bb=Decimal.TryParse(jiage,out jia);

            if (jiage== ""||!bb)
            {
                Response.Write("<script >function window.onload() {alert('请填写图书价格!');}</script>");
                return;
            }
            string jianjie = this.manhuajianjie.Text;
            if (jianjie == "")
            {
                Response.Write("<script >function window.onload() {alert('请填写图书简介!');}</script>");
                return;
            }
           
           

            if (this.fengmiantupian.HasFile)
            {
                string filetype = this.fengmiantupian.PostedFile.ContentType;
                if (filetype == "image/x-png")
                {
                    //有图片
                }
                else
                {
                    Response.Write("<script >function window.onload() {alert('图片格式需为png!');}</script>");
                    return;
                }

            }
            else
            {
                Response.Write("<script >function window.onload() {alert('请选择封面图片!');}</script>");
                return;
            }

            if (this.tushupdf.HasFile)
            {

                string filetype = this.tushupdf.PostedFile.ContentType;
                if (filetype == "application/pdf")
                {
                    //有pdf文件
                }
                else
                {
                    Response.Write("<script >function window.onload() {alert('格式图书需为pdf!');}</script>");
                    return;
                }

            }
            else
            {
                Response.Write("<script >function window.onload() {alert('请选择图书pdf文件!');}</script>");
                return;
            }

            //接下来存到数据库中，并把文件存起来

            MySqlConnection conn = new MySqlConnection("Data Source=127.0.0.1;Initial Catalog=test;User ID=root;Password=123456");

            conn.Open();

            if (conn.State.ToString() == "Open")
            {
                try
                {

                    MySqlCommand com = new MySqlCommand();
                    com.Connection = conn;
                    com.CommandText = "insert into yilanmanhua(fenlei,zuozhe,mingcheng,riqi,jiage,jianjie) values (?cfenlei,?czuozhe,?cmingcheng,?criqi,?cjiage,?cjianjie)";
                    com.Parameters.AddWithValue("cfenlei",fenlei);
                    com.Parameters.AddWithValue("czuozhe", zuozhe);
                    com.Parameters.AddWithValue("cmingcheng",mingcheng);
                   
                    com.Parameters.AddWithValue("criqi",datett);
                    com.Parameters.AddWithValue("cjiage",(float)jia);
                    com.Parameters.AddWithValue("cjianjie",jianjie);
                    

                    int i = com.ExecuteNonQuery();

                    if (i > 0) {

                        com.CommandText = "select max(id) from yilanmanhua";
                        int maxid = (int)com.ExecuteScalar();

                        string picname = fengmiantupian.PostedFile.FileName;

                        FileInfo file = new FileInfo(picname);
                        string filename = file.Name;

                        string picpath = Server.MapPath("picture/" +maxid+".png");
                        fengmiantupian.SaveAs(picpath);

                        string pdfpath = Server.MapPath("pdf/" + maxid + ".pdf");
                        tushupdf.SaveAs(pdfpath);
                    
                    }
                    conn.Close();
                }
                catch (MySqlException e1) {
                    conn.Close();
                    Response.Write(e1.Message);
                    
                }

                
            }


        }


    }
}